package com.bicon.botu.medical.api


import com.bicon.botu.core.base.common.Constants
import com.bicon.botu.core.base.common.controller.BaseController
import com.bicon.botu.core.base.common.entity.BaseResult
import com.bicon.botu.core.base.common.utils.StringUtils
import com.bicon.botu.medical.common.utils.Encrypt
import com.bicon.botu.medical.model.dto.UserAccountParam
import com.bicon.botu.medical.model.dto.UserInfoParam
import com.bicon.botu.medical.model.entity.Account
import com.bicon.botu.medical.service.IAccountService
import com.bicon.botu.medical.service.IUserService
import io.swagger.annotations.ApiImplicitParam
import io.swagger.annotations.ApiOperation
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController

@Slf4j
@RestController
@RequestMapping("/api/userdata")
/**
 * @author gdd
 *
 */
open class PersonalApi : BaseController() {


    @Autowired
    open lateinit var userService: IUserService

    @Autowired
    open lateinit var accountService: IAccountService

    /**
     * 修改用户资料
     * @return
     */
    @RequestMapping(value = "/update", method = arrayOf(RequestMethod.POST))
    fun updateUserInfo(@RequestParam userId: String,
                       @RequestParam(required = false) nickName: String?,
                       @RequestParam(required = false) largeImg: String?,
                       @RequestParam(required = false) smallImg: String?,
                       @RequestParam(required = false) gender: Int?,
                       @RequestParam(required = false) birthday: String?,
                       @RequestParam(required = false) weight: Double?,
                       @RequestParam(required = false) height: Double?): BaseResult<*> {
        val userInfoParam = UserInfoParam()
        if (!StringUtils.isEmpty(userId)) {
            userInfoParam.id = userId
        } else {
            return BaseResult<String>(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400)
        }
        if (!StringUtils.isEmpty(nickName)) {
            userInfoParam.nickName = nickName
        }
        if (!StringUtils.isEmpty(weight)) {
            userInfoParam.weight = weight
        }
        if (!StringUtils.isEmpty(height)) {
            userInfoParam.height = height
        }
        if (!StringUtils.isEmpty(largeImg)) {
            userInfoParam.largeImg = largeImg
        }
        if (!StringUtils.isEmpty(smallImg)) {
            userInfoParam.smallImg = smallImg
        }
        if (!StringUtils.isEmpty(gender)) {
            userInfoParam.gender = gender
        }
        if (!StringUtils.isEmpty(birthday)) {
            userInfoParam.birthday = birthday
        }
        return if (userService!!.updatePersonData(userInfoParam)) {
            sendResult200()
        } else sendResult500()
    }

    /**
     * 是否是电话号码
     */
    fun String.isPhone(): Boolean {
        val p = "^1([34578])\\d{9}\$".toRegex()
        return matches(p)
    }

    /**
     * 根据uid查询用户信息
     *
     * @param userId 用户id
     * @return
     */
    @ApiOperation(value = "根据userId查询用户信息", notes = "根据userId查询用户信息")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @RequestMapping(value = "/detail", method = arrayOf(RequestMethod.GET))
    fun findByUid(@RequestParam userId: String): BaseResult<*> {
        return sendResult200(userService!!.getPersonData(userId))
    }

    /**
     * 移动端发送验证码---更换手机号   修改登录密码
     *
     * @param account 账号
     * @param flag    1:更换手机号 ,2修改登录密码
     * @return
     */
    @RequestMapping(value = "/sendSms", method = arrayOf(RequestMethod.POST))
    fun sendSms(@RequestParam phone: String, @RequestParam flag: Int?): BaseResult<*> {
        if (StringUtils.isEmpty(phone)) {
            return BaseResult<Any>(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400)
        }

            //校验手机号格式
            if (!phone!!.isPhone()) {
                return BaseResult<String>(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400)
            }
        //试并返回，不走短信平台
        return sendResult200(userService.sendSmsCodeString(phone, 1))
        //正式环境需要放开下面代码，
             /*   if (userService.sendSmsCode(phone, 1)) {
                    return sendResult200();
                }
                return sendResult500();*/
    }

    /**
     * 更换手机号
     *
     *  @param userId  用户id
     * @param phone 新手号
     * @param code 验证码
     * @return
     */
    @RequestMapping(value = "/changePhone", method = arrayOf(RequestMethod.POST))
    fun changePhone(@RequestParam userId: String,@RequestParam phone: String, @RequestParam code: String): BaseResult<*> {
        //校验该账号是否存在
        val  account = accountService.getAccountById(userId) ?: return BaseResult<Any>(Constants.RESPONSE_CODE_602, "该账号不存在！")
        if (account.enable==1) {
            //判断账号的状态
            return BaseResult<Any>(Constants.RESPONSE_CODE_405, "该账号已被禁用！")
        }
        //校验手机验证码是否正确、过期
        //检查验证码是否正确
        if (!userService.verifySmsCode(phone, code, 1)) {
            return BaseResult<Any>(Constants.RESPONSE_CODE_405, "验证码错误或失效！")
        }
        //更换手机号
        val user =  UserInfoParam()
        user.phone=phone
        user.id = userId
        val flag = userService.updatePersonData(user)
        //跟换账号
        val userAccount = UserAccountParam()
        userAccount.account =phone
        userAccount.id=userId
        val flag1 =accountService.updateAccount(userAccount)
        if(flag&&flag1){
            return BaseResult<Any>(Constants.RESPONSE_CODE_200, "修改手机号成功！")
        }
        return BaseResult<Any>(Constants.RESPONSE_CODE_500, "未知错误")
    }

    /**
     * 修改密码
     *
     * @param phone  账号
     * @param password 密码
     * @param code     验证码
     * @return
     */
    @RequestMapping(value = "/forgetpwd", method = arrayOf(RequestMethod.POST))
    fun forgetPassword(@RequestParam userId: String, @RequestParam phone: String, @RequestParam password: String, @RequestParam code: String): BaseResult<*> {
        //检查账号是否存在
        val account = accountService.getAccountById(userId) ?: return BaseResult<Any>(Constants.RESPONSE_CODE_602, "该账号不存在！")
        if (account.enable==1) {
            //判断账号的状态
            return BaseResult<Any>(Constants.RESPONSE_CODE_405, "该账号已被禁用！")
        }
        //校验手机验证码是否正确、过期
        //检查验证码是否正确
        if (!userService.verifySmsCode(phone, code, 1)) {
            return BaseResult<Any>(Constants.RESPONSE_CODE_405, "验证码错误或失效！")
        }
        //修改密码操作
        return if (userService.forgetPassByAccout(phone, Encrypt.md5ForAuth(password, account.salt), 1)) {
            sendResult200()
        } else sendResult500()
    }

}